package com.park.web.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.client.model.news.NewsModel;
import com.park.client.service.news.NewsService;
import com.park.common.util.Page;
import com.park.common.util.PageData;
import com.park.common.util.Result;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;

@Controller
@RequestMapping("/admin/news")
public class AdminNewsController extends BaseController{

	@Autowired
	private NewsService newsService;
	
	@RequestMapping("listRows")
	@ResponseBody
	public PageData<NewsModel> listRows(Integer rows,@RequestParam(name="page",defaultValue="1")Integer pageNow,HttpServletRequest request){
		PageData<NewsModel>  pageData = new PageData<>();
		NewsModel news = new NewsModel();
		Integer total = newsService.selectTotal(news);
		Page page = new Page(pageNow,total,rows);
		news.setStartPos(page.getStartPos());
		news.setPageSize(page.getPageSize());
		List<NewsModel> list = newsService.selectPagination(news);
		pageData.setTotal(total);
		pageData.setRows(list);
		return pageData;
	}
	
	@RequestMapping("newsList")
	public String newsList(HttpServletRequest request){
			return Global.ADMIN_PATH+"news/newsList";
	}
	
	@RequestMapping("save")
	@ResponseBody
	public Result<Boolean> save(NewsModel news,HttpServletRequest request){
		Result<Boolean> result = new Result<>();
		boolean success = newsService.insertSelective(news);
		if(success){
			result.setMessage("添加成功!");
			result.setSuccess(true);
		}else{
			result.setSuccess(false);
			result.setMessage("添加失败!");
		}
		return result;
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public Result<Boolean> deleteById(@RequestParam("id") Long id,HttpServletRequest request){
		Result<Boolean> result = new Result<>();
		boolean success = newsService.deleteByPrimaryKey(id);
		if(success){
			result.setMessage("删除成功!");
			result.setSuccess(true);
		}else{
			result.setMessage("删除失败!");
			result.setSuccess(false);
		}
		return result;
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public Result<Boolean> edit(NewsModel news,HttpServletRequest request){
		Result<Boolean> result = new Result<>();
		boolean success = newsService.updateByPrimaryKeySelective(news);
		if(success){
			result.setMessage("修改成功!");
			result.setSuccess(true);
		}else{
			result.setMessage("修改失败!");
			result.setSuccess(false);
		}
		return result;
	}
	
}
